
Ext.define('DCApp.EntitySimlet', {
    extend: 'Ext.ux.ajax.JsonSimlet',
    alias: 'simlet.entity',
    
    doPost: function(ctx) {
        var result = this.callParent(arguments),
            o = this.processData(Ext.decode(ctx.xhr.body)),
            item = this.getById(this.data, o.id, true),
            key;
        
        for (key in o) {
            item[key] = o[key];
        }
        
        result.responseText = Ext.encode(item);
        return result;
    },
    
    processData: Ext.identityFn,

    getData: function (ctx) {
        var params = ctx.params;

        if ('id' in params) {
            return this.getById(this.data, params.id);
        }

        delete this.currentOrder;
        return this.callParent(arguments);
    },
    
    getById: function(data, id) {
        var len = data.length,
            i, item;
        
        for (i = 0; i < len; ++i) {
            item = data[i];
            if (item.id === id) {
                return item;
            }
        }
        return null;
    }
});

Ext.define('DCApp.SimData', {
    requires: [
        'Ext.ux.ajax.*'
    ],

    singleton: true,

    dateFormat: 'Y-m-d\\TH:i:s\\Z',


    init: function () {
    	function uncapitalize(text) {
            if(!text || typeof text !== "string") {
               return ''; 
            }

            return text.charAt(0).toLowerCase() + text.substr(1);
        }
        function makeSim (data) {
            return {
                type: 'entity',
                data: data
            };
        }
     var me = this;
     var enm=DCApp.Cfg.SIM_ENM;
     var sim_ens={};
  	for(var l in enm){
	var data = [];
	var en=l;
    var om = Ext.create("DCApp.model."+en);
    for(var i=0; i<enm[l]; i++){
    	data.push(om.randData(i));
    }
    var url = '/rest/'+en;
    if(en=='Image'){
    	sim_ens[url]= Ext.apply({
             processFilters: function(filters) {
                 Ext.each(filters, function (filter, index) {
                     if (filter.property == 'docId' || filter.property == 'relicId' 
                     || filter.property == 'buildingId' 
                     || filter.property == 'catalogId'
                     || filter.property == 'figureId') {
                         filters[index] = function (r) {
                             return r.imageId <filter.value;
                         }
                     }
                 });
                 return this.self.prototype.processFilters.call(this, filters);
             }
         }, makeSim(data));
    } else if(en=='CeAttachment'){
    	sim_ens[url] = Ext.apply({
            processFilters: function(filters) {
                Ext.each(filters, function (filter, index) {
                    if (filter.property == 'docId' 
                    || filter.property == "ceGiftId" 
                    || filter.property == 'buildingId'
                    || filter.property == 'figureId') {
                        filters[index] = function (r) {
                            return r.id <filter.value;
                        }
                    }
                });
                return this.self.prototype.processFilters.call(this, filters);
            }
        }, makeSim(data));
   } else if(en=='CeGift'){
    	sim_ens[url] = Ext.apply({
            processFilters: function(filters) {
                Ext.each(filters, function (filter, index) {
                    if (filter.property == 'docId') {
                        filters[index] = function (r) {
                            return r.id <filter.value;
                        }
                    }
                });
                return this.self.prototype.processFilters.call(this, filters);
            }
        }, makeSim(data));
   } else if(en=='Figure'){
    	sim_ens[url] = Ext.apply({
            processFilters: function(filters) {
                Ext.each(filters, function (filter, index) {
                    if (filter.property == 'docId') {
                        filters[index] = function (r) {
                            return r.id <filter.value;
                        }
                    }
                });
                return this.self.prototype.processFilters.call(this, filters);
            }
        }, makeSim(data));
   } else if(en=='AudioVideo'){
    	sim_ens[url] = Ext.apply({
            processFilters: function(filters) {
                Ext.each(filters, function (filter, index) {
                    if (filter.property == 'docId') {
                        filters[index] = function (r) {
                            return r.id <filter.value;
                        }
                    }
                });
                return this.self.prototype.processFilters.call(this, filters);
            }
        }, makeSim(data));
   }else if(en=='BaseAuthor'){
    	sim_ens[url] = Ext.apply({
            processFilters: function(filters) {
                Ext.each(filters, function (filter, index) {
                    if (filter.property == 'docId') {
                        filters[index] = function (r) {
                            return r.id <filter.value;
                        }
                    }
                });
                return this.self.prototype.processFilters.call(this, filters);
            }
        }, makeSim(data));
   } else if(en == 'Thesis') {
    	sim_ens[url] = Ext.apply({
            processFilters: function(filters) {
               Ext.each(filters, function (filter, index) {
                    if (filter.property == 'relicId' || filter.property == 'buildingId' || filter.property == 'catalogId'|| filter.property == 'docId') {
                        filters[index] = function (r) {
                            return r.thesisId < filter.value;
                        }
                    }
                });
                return this.self.prototype.processFilters.call(this, filters);
            }
        }, makeSim(data));
   } else if(en == 'Building') {
    	sim_ens[url] = Ext.apply({
            processFilters: function(filters) {
               Ext.each(filters, function (filter, index) {
                    if ( filter.property == 'docId') {
                        filters[index] = function (r) {
                            return r.objId < filter.value;
                        }
                    }
                });
                return this.self.prototype.processFilters.call(this, filters);
            }
        }, makeSim(data));
   } else if(en == 'Relic') {
    	sim_ens[url] = Ext.apply({
            processFilters: function(filters) {
               Ext.each(filters, function (filter, index) {
                    if (filter.property == 'docId' || filter.property == 'buildingId') {
                        filters[index] = function (r) {
                            return r.objId < filter.value;
                        }
                    }
                });
                return this.self.prototype.processFilters.call(this, filters);
            }
        }, makeSim(data));
   }  else if(en == 'Doc') {
   	sim_ens[url] = Ext.apply({
        processFilters: function(filters) {
           Ext.each(filters, function (filter, index) {
                if (filter.property == 'relicId' || filter.property == 'buildingId'|| filter.property == 'docId') {
                    filters[index] = function (r) {
                        return r.id < filter.value;
                    }
                }
            });
            return this.self.prototype.processFilters.call(this, filters);
        }
    }, makeSim(data));
}else
    	sim_ens[url]=makeSim(data);
   }
  	sim_ens['/authenticate']= {
        type: 'json',
        data: function(ctx) {
            var userName = ctx.params.username;
            return Ext.create('DCApp.model.User',{name:userName,dpt:'Cassell'});
        }
    }
  DCApp.Cfg.SM =Ext.ux.ajax.SimManager.init({defaultSimlet:null}).register(sim_ens);
  }
});
